/**
 * @author caojj
 * @version 1.0.9.5
 * @history 
 * 		1.0.2.0		# 参数较少时无法post发送的问题	@20080411
 * 		1.0.3.0		# 初始化时不传参数报错 @20080412
 * 		1.0.4.1		# 单独引用文件时没有定义FWK空间的错误 @20080412
 * 		1.0.5.0		+ 创建XMLDocument的方法	@20080416
 * 		1.0.6.0		+ 回调完成后的$finally方法 @20080729
 * 					# 命名规范 @20080729
 * 					+ 回调异常的调试方式 ajax.debug = true; @20080729
 * 		1.0.7.0		+ setParameterObj 可在初始化后修改parameterObj @20080801
 * 		1.0.8.0		# setParamObj @20080804
 * 					+ getParamObj @20080804
 * 		1.0.9.0		# 完善了调试功能 @20080807
 */
try {
	if(!FWK.Util){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.Util = {}
}

FWK.Util.Ajax=function (aURL, aTransType, parameterObj, isAsync){
	var XHO;
	var me=this;
	
	this.debug = false;
	
	this.aURL = aURL;
	this.aTransType = aTransType;
	this.isAsync = isAsync;
	this.paramObj = parameterObj;
	this.parameterStr="";	//((aURL.indexOf("?")>-1)?"&r=":"?r=")+Math.random(1000)+"&";
	
//	try { XHO = new ActiveXObject("Msxml2.XMLHTTP"); }
//	catch (e) { try { XHO = new ActiveXObject("Microsoft.XMLHTTP"); }
//	catch (e) { try { XHO = new XMLHttpRequest(); }
//	catch (e) { XHO = false; }}}

	XHO = FWK.Util.GenXmlHttpRequest();

	if (!XHO) return null;

	this.successed = false;
	this.responseXML = null;
	this.responseText = null;

	this.gc=function() {
		try {
			me.dispose();
			delete me;
		} catch (ex) {
			me = null;
		}
	}

	this.request=function(aURL, aTransType, parameterObj, isAsync){
		me.aURL = aURL;
		me.aTransType = aTransType;
		me.parameterStr=((aURL.indexOf("?")>-1)?"&r=":"?r=")+Math.random(1000)+"&";
		me.paramObj = parameterObj;
		
		if (isAsync&&isAsync=="async"||isAsync==true){me.isAsync=true;}else{me.isAsync=false;}

		if (parameterObj) {
			if (typeof(parameterObj)=="string") {
				me.parameterStr=me.parameterStr+parameterObj;
			} else {
				for (var i in parameterObj){
					if (parameterObj[i]) {
						parameterObj[i] = parameterObj[i].toString().replace(/\]\]>/g, "] >");
					}
					me.parameterStr =me.parameterStr+i+"="+encodeURIComponent(parameterObj[i])+"&";
				}
			}
		}
		
//		me.parameterStr += "&" + Math.random(1000);
//		if (parameterObj && isAsync){
//			me.parameterStr="";
//			for (var i in parameterObj){
//				me.parameterStr =me.parameterStr+i+"="+encodeURIComponent(parameterObj[i])+"&";
//			}
//		}else if(parameterObj){
//			me.parameterStr=me.parameterStr+parameterObj;
//		}
//		me.parameterStr+="&"+_Random(0,100000);
//		me.parameterStr = me.parameterStr.replace(/[[\x00-\x20]^\x09\x10\x13]/g,"*");	//换掉xml中无法解析的特殊符号
		
	}

	this.open = function(aURL, aTransType, parameterObj, isAsync) {
		if (me.successed) {
			me.dispose();
		}
		
		if(arguments.length) {
			me.request(aURL, aTransType, parameterObj, isAsync);
		}
		
		var aURL = me.aURL;
		var aTransType = me.aTransType;
		var parameterObj = me.parameterObj;
		var isAsync = me.isAsync;
		
		try{
			if(aTransType&&aTransType.toUpperCase()=="POST"){
				XHO.open("POST", aURL, isAsync);
				XHO.setRequestHeader("Method", "POST "+aURL+" HTTP/1.1");
				XHO.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				
//				XHO.send(me.parameterStr);
			}else{
				XHO.open("GET", aURL+me.parameterStr, isAsync);
				me.parameterStr = null;
			}

			if (me.parameterStr) {
				me.parameterStr = me.parameterStr.replace(/[\x00-\x08\x11-\x12\x14-\x20]/g,"*");
				me.parameterStr = me.parameterStr.replace(/\]\]>/g, "] >");
			}						
			if(XHO.overrideMimeType){XHO.overrideMimeType('text/xml');}
			XHO.send(me.parameterStr);
			
			if (isAsync){
				XHO.onreadystatechange = function(){
					if (XHO.readyState == 4) {
						if (XHO.status == 200) {
							try {
								tempDOM = new ActiveXObject('Microsoft.XMLDOM');
								tempDOM.loadXML(XHO.responseText);
							} catch (e) {
								var tempDOM = XHO.responseXML;
							}
							
							me.responseXML = tempDOM;
							me.responseText = XHO.responseText;
							me.successed = true;//alert(me.responseXML.xml);
							try {
								if (me.debug) {
									success();
								}
								me.$success();								
							} catch(ex) {
								if (me.debug) {
									alert(ex.message||ex)
								}
							} finally {
								me.$finally();
								setTimeout(me.gc);								
							}
							return me;
						} else {
							me.successed = false;
							try {
								if (me.debug) {
									fail();
								}
								me.$fail();
							} catch(ex) {
								if (me.debug) {
									alert(ex.message||ex)
								}
							} finally {
								me.$finally();
								me.gc();								
							}
						}
					}
				}
			} else {
				if (XHO.status == 200) {
					try{
						tempDOM = new ActiveXObject('Microsoft.XMLDOM');
						tempDOM.loadXML(XHO.responseText);
					}catch(e){
						var tempDOM=XHO.responseXML;
					};
					me.responseXML=tempDOM;
					me.responseText=XHO.responseText;
					me.successed=true;//alert(me.responseXML.xml)
					if (me.debug) {
						success();
					}
					return me;
				} else {
					if (me.debug) {
						fail();
					}	
					me.successed=false;
				}
			}
			
			
//			me.parameterStr = me.parameterStr.replace(/[\x00-\x08\x11-\x12\x14-\x20]/g,"*");
//			XHO.send(me.parameterStr);

		}catch(e){return false;}
	}

	//cancel the request
	this.abort = function() {
		XHO.abort();
	}

	//ready for overwrite
	this.$success = function() {
//		success();
	}
	function success() {
		var str = "aURL:" + me.aURL;
			str += "\naTransType: " + me.aTransType;
			str += "\nParameterStr: " + me.parameterStr;			
			str += "\nIsAsync: "+ me.isAsync;
			str += "\nResponse: \n\n";
			str += me.responseXML.xml || me.responseText;
			
		alert(str);
		return str;
	}
	//ready for overwrite
	this.$fail = function() {
//		fail();
	}
	function fail() {
		var str = "aURL: " + me.aURL;
			str += "\naTransType: " + me.aTransType;
			str += "\nParameterStr: " + me.parameterStr;
			str += "\nIsAsync: "+ me.isAsync;
			str += "\nHttp status: " + XHO.status || "unKnown";
			str += "\nRequest Readystat: " + XHO.readyState || "unKnown";
			
		alert(str);
		return str;
	}
	//ready for overwrite
	this.$finally = function() {
		
	}
	
	this.setParamObj = function(obj) {
		me.dispose();		
		me.request(me.aURL, me.aTransType, obj, me.isAsync);
	}
	
	this.getParamObj = function() {
		return me.paramObj;
	}

	{
		try {
			this.request(aURL, aTransType, parameterObj, isAsync);
		} catch (ex) {
		}
	}
	
	this.dispose = function() {
		me.successed = false;
		me.responseXML = null;
		me.responseText = null;
		
//		XHO.responseText = null;
//		XHO.responseXML = null;
	}
}
FWK.Util.Ajax.prototype.enableDebug = function() {
	this.debug = true;
}


FWK.Util.GenXmlHttpRequest = function() {
	var XHO=null;
	try { XHO = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { XHO = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { XHO = new XMLHttpRequest(); }
	catch (e) { XHO = false; }}}
	return XHO;
}

FWK.Util.GenXmlDocument = function() {
	var XMLDoc = null;
	try {
		XMLDoc = new ActiveXObject("Microsoft.XMLDOM");
	} catch(e) {
		try {
			XMLDoc = document.implementation.createDocument("text/xml", "", null); //new XMLDocument();
		}catch(e){
			XMLDoc = false;
		}
	}
	return XMLDoc;
}
